<template>
	<view class="add-relatives-parent">
		<view style="width: 100%;background-color: #F5F6FA;height: 2rpx;"></view>
		<view style="justify-content: center;align-items: center;flex-direction: column;display: flex;">
			<button class="head-view" type="primary" @click="getHeadImg" style="background-color:transparent;background:transparent;border-color: transparent;">
				<image class="head-image" :src="headImg" mode="scaleToFill"></image>
			</button>

			<text class="chengwei">{{gxname}}</text>
			<view class="xing-ming-view">
				<view class="item-view">
					<text class="item-tv">姓</text>
					<input class="item-input" placeholder="请输入姓" :value="xingStr" @input="xingInput" />
				</view>
				<view class="item-view" style="margin-left: 28rpx;">
					<text class="item-tv">名</text>
					<input class="item-input" placeholder="请输入名" :value="mingStr" @input="mingInput" />
				</view>
			</view>
			<view style="flex: 1;flex-direction: column;display: flex;width: 100%;">
				<text class="gengduo-info-txt">更多资料（选填）</text>
			</view>
			<view class="list-item" style="margin-top: 20rpx;" @click="timeSelect" data-item="1">
				<text class="list-item-title">生日</text>
				<text class="list-flex-view"></text>
				<text class="list-item-content">{{borTime}}</text>
				<image class="list-item-arrow" src="../../static/right-arrow.png"></image>
			</view>
			<view class="list-item" @click="addressSelect" data-item="4">
				<text class="list-item-title">祖籍地</text>
				<text class="list-flex-view"></text>
				<text class="list-item-content">{{zujiAddress}}</text>
				<image class="list-item-arrow" src="../../static/right-arrow.png"></image>
			</view>
			<view v-if="isAlive" class="list-item" @click="addressSelect" data-item="5">
				<text class="list-item-title">现居地</text>
				<text class="list-flex-view"></text>
				<text class="list-item-content">{{xianjuAddress}}</text>
				<image class="list-item-arrow" src="../../static/right-arrow.png"></image>
			</view>
			<view class="list-item" @click="timeSelect" data-item="2">
				<text class="list-item-title">结婚纪念日</text>
				<text class="list-flex-view"></text>
				<text class="list-item-content">{{annTime}}</text>
				<image class="list-item-arrow" src="../../static/right-arrow.png"></image>
			</view>
			<!-- 		<view class="list-item" @click="changeZhiYe">
					<text class="list-item-title">职业</text>
					<text class="list-flex-view"></text>
					<text class="list-item-content">{{zhiye}}</text>
					<image class="list-item-arrow" src="../../static/right-arrow.png"></image>
				</view> -->
			<view v-if="isShowAlive" class="list-item">
				<text class="list-item-title">在世</text>
				<text class="list-flex-view"></text>
				<text class="list-item-content"></text>
				<switch class="switch-btn" @change="switch2Change" :checked="isAlive" color="#FF4206" />
			</view>
			<view v-if="!isAlive" class="list-item" @click="timeSelect" data-item="3">
				<text class="list-item-title">祭日</text>
				<text class="list-flex-view"></text>
				<text class="list-item-content">{{dadTime}}</text>
				<image class="list-item-arrow" src="../../static/right-arrow.png"></image>
			</view>
			<view v-if="!isAlive" class="list-item" style="margin-bottom: 20rpx;" @click="addressSelect" data-item="6">
				<text class="list-item-title">墓地</text>
				<text class="list-flex-view"></text>
				<text class="list-item-content">{{mudiAddress}}</text>
				<image class="list-item-arrow" src="../../static/right-arrow.png"></image>
			</view>
			<text class="tijiao" @click="AddTreeMerber">完成</text>
		</view>
		<view class="chose-address-view">
			<choseAddress :isShow="isChoseAddressShow" @returnAddress="addressReturn"></choseAddress>
		</view>
		<inputDalog :isShow="isZhiYeDialogShow" :dataSource="zhiyeData" @returnInputDialog="inputDialogReturn"></inputDalog>
		<headChoosePop :isShow="isShowChooseHeadPop" :dataSource="popGener"  @returnHeadChoose="returnHeadChoose"/>
	</view>
</template>

<script>
	import {
		getLunar
	} from 'chinese-lunar-calendar'
	import choseTime from "@/components/chose-time/time-chose.vue";
	import choseAddress from '../../components/chose-address/chose-address.vue'
	import inputDalog from '../../components/input-dialog/input-dialog.vue'
	import tipDialog from '../../components/tip-dialog/tip-dialog.vue'
	import request from '../index/request.js';
	import headChoosePop from "@/components/head-choose-pop/head-choose-pop.vue";
	
	export default {
		components: {
			choseTime,
			choseAddress,
			inputDalog,
			tipDialog,
			headChoosePop
		},
		data() {
			return {
				isShowChooseHeadPop:false,
				isShowAlive: true,
				binding: "0",
				selectId: "",
				relation: "",
				//关系值
				gxval: "",
				//称谓
				gxname: "",
				treeId: "",
				isAuth: "1",
				type: -2,
				headImg: "",
				headImgUpload: "",
				xingStr: "",
				mingStr: "",
				isAlive: true,
				isChoseAddressShow: false,
				timeType: "0",
				addressType: "0",
				borTime: "",
				borTime1: 0,
				borTime2: 0,
				borTime3: 0,
				annTime: "",
				annTime1: "",
				annTime2: 0,
				annTime3: 0,
				dadTime: "",
				dadTime1: "",
				dadTime2: 0,
				dadTime3: 0,
				zujiAddress: "",
				xianjuAddress: "",
				mudiAddress: "",
				zhiye: "",
				isZhiYeDialogShow: false,
				zhiyeData: {
					title: "职业",
					hint: "请填写职业",
					content: "sssssssssssssssssssssss",
					leftTxt: "取消",
					rightTxt: "确认",
					tag: "zhiye",
				},
				lastPageDataSurname: "",
				lastPageDataIsSpouse: "",
				spouseSurname: "",
				lastPageDataGener: "",
				popGener:0,
			}
		},
		onUnload:function(){
			uni.$off("selectTimeReturn")
		},
		methods: {
			onShow: function() {
				this.share.url = '/pages/tabBar/home/index?appInviteId=' + this.appUtil.getAppInviteId()
				this.share.shareType = 1
			},
			onLoad: function(options) {
				this.appUtil.saveAppInviteId(options)
				this.type = options.type
				this.treeId = options.treeId
				this.binding = options.binding
				this.gxname = options.gxname
				this.gxval = options.gxval

				this.selectId = options.selectId
				this.spouseSurname = options.spouseSurname
				this.lastPageDataSurname = options.dataSurname
				this.lastPageDataIsSpouse = options.dataIsSpouse
				this.lastPageDataGener = options.dataGener
				console.log(JSON.stringify(options))
				uni.setNavigationBarTitle({
					title: "添加亲人"
				})
				if (this.binding === '0') {
					this.isShowAlive = true
				} else {
					if (this.gxname === '本人') {
						this.isShowAlive = false
					} else {
						this.isShowAlive = true
					}
				}
				switch (this.type) {
					case "0":
						//父亲
						this.xingStr = this.lastPageDataSurname
						this.headImg = "https://c.shiqin.wang/assets/img/man_ws.png"
						this.relation = "6"
						this.popGener=1
						if (this.binding === '1') {
							this.getTreeRelationCall("6")
						}
						break
					case "-1":
						//丈夫
						this.xingStr = ''
						this.headImg = "https://c.shiqin.wang/assets/img/man_ws.png"
						this.relation = "1"
						this.popGener=1
						if (this.binding === '1') {
							this.getTreeRelationCall("1")
						}
						break
					case "3":
						//兄
						this.xingStr = this.lastPageDataSurname
						this.headImg = "https://c.shiqin.wang/assets/img/man_ws.png"
						this.relation = "2"
						this.popGener=1
						if (this.binding === '1') {
							this.getTreeRelationCall("2")
						}
						break
					case "4":
						//弟
						this.xingStr = this.lastPageDataSurname
						this.headImg = "https://c.shiqin.wang/assets/img/man_ws.png"
						this.relation = "3"
						this.popGener=1
						if (this.binding === '1') {
							this.getTreeRelationCall("3")
						}
						break
					case "7":
						//儿子
						if (this.lastPageDataIsSpouse == '0') {
							//非配偶
							if (this.lastPageDataGener == '0') {
								this.xingStr = ""
							} else {
								this.xingStr = this.lastPageDataSurname
							}
						} else {
							//配偶
							if (this.lastPageDataGener == '0') {
								this.xingStr = this.spouseSurname
							} else {
								this.xingStr = this.lastPageDataSurname
							}
						}
						this.headImg = "https://c.shiqin.wang/assets/img/man_ws.png"
						this.relation = "8"
						this.popGener=1
						if (this.binding === '1') {
							this.getTreeRelationCall("8")
						}
						break
					case "1":
						//母亲
						this.xingStr = ''
						this.headImg = "https://c.shiqin.wang/assets/img/women_ws.png"
						this.relation = "7"
						this.popGener=0
						if (this.binding === '1') {
							this.getTreeRelationCall("7")
						}
						break
					case "2":
						//妻子
						this.xingStr = ''
						this.headImg = "https://c.shiqin.wang/assets/img/women_ws.png"
						this.relation = "0"
						this.popGener=0
						if (this.binding === '1') {
							this.getTreeRelationCall("0")
						}
						break
					case "5":
						//姐
						this.xingStr = this.lastPageDataSurname
						this.headImg = "https://c.shiqin.wang/assets/img/women_ws.png"
						this.relation = "4"
						this.popGener=0
						if (this.binding === '1') {
							this.getTreeRelationCall("4")
						}
						break
					case "6":
						//妹
						this.xingStr = this.lastPageDataSurname
						this.headImg = "https://c.shiqin.wang/assets/img/women_ws.png"
						this.relation = "5"
						this.popGener=0
						if (this.binding === '1') {
							this.getTreeRelationCall("5")
						}
						break
					case "8":
						//女儿
						if (this.lastPageDataIsSpouse == '0') {
							//非配偶
							if (this.lastPageDataGener == '0') {
								this.xingStr = ""
							} else {
								this.xingStr = this.lastPageDataSurname
							}
						} else {
							//配偶
							if (this.lastPageDataGener == '0') {
								this.xingStr = this.spouseSurname
							} else {
								this.xingStr = this.lastPageDataSurname
							}
						}
						this.headImg = "https://c.shiqin.wang/assets/img/women_ws.png"
						this.relation = "9"
						this.popGener=0
						if (this.binding === '1') {
							this.getTreeRelationCall("9")
						}
						break
					case "9":
						//男
						this.xingStr = ''
						this.headImg = "https://c.shiqin.wang/assets/img/man_ws.png"
						this.relation = "3"
						this.popGener=1
						break
					case "10":
						//女
						this.xingStr = ''
						this.headImg = "https://c.shiqin.wang/assets/img/women_ws.png"
						this.relation = "4"
						this.popGener=0
						break
				}
				var that = this
				uni.$on("selectTimeReturn", (data) => {
					that.timeReturn(data)
				})
			},
			inputDialogReturn: function(event) {
				this.isZhiYeDialogShow = event.isclose
				if (event.inputStr) {
					this.zhiye = event.inputStr
				}
			},
			returnHeadChoose:function(e){
				this.isShowChooseHeadPop=e.isclose
				if(e.tag){
					if(e.position!=0){
						this.headImgUpload = e.position
						switch(e.position){
							case 1:
								if(this.popGener==0){
									this.headImg = "http://c.shiqin.wang/photo/nvyi.png"
								}else{
									this.headImg = "http://c.shiqin.wang/photo/nanyi.png"
								}
								break
							case 2:
								if(this.popGener==0){
									this.headImg =	"http://c.shiqin.wang/photo/nver.png"
								}else{
									this.headImg = "http://c.shiqin.wang/photo/naner.png"
								}
								break
							case 3:
								if(this.popGener==0){
									this.headImg = "http://c.shiqin.wang/photo/nvsan.png"
								}else{
									this.headImg = "http://c.shiqin.wang/photo/nansan.png"
								}
								break
						}
					}else{
						var that = this
						uni.chooseImage({
							count: 1, // 最多可以选择的图片张数，默认9
							sizeType: ['original', 'compressed'], //original 原图，compressed 压缩图，默认二者都有
							sourceType: ['album', 'camera'], //album 从相册选图，camera 使用相机，默认二者都有。如需直接开相机或直接选相册，请只使用一个选项
							success: function(res) {
								uni.uploadFile({
									url: "https://a.shiqin.wang/addons/alioss/index/upload",
									filePath: res.tempFilePaths[0],
									name: 'file',
									success: function(result) {
										if (result.statusCode == 200) {
											let headData = JSON.parse(decodeURI(result.data))
											that.headImg = headData.data.url_s
											that.headImgUpload = headData.data.url
										}
									}
								})
							}
						});
					}
				}
			},
			getHeadImg: function() {
				this.isShowChooseHeadPop=true
			},
			// 获取人物关系值
			getTreeRelationCall: function(basicsRelation) {
				this.relation = basicsRelation
				var that = this
				request.post("api/apiv2/GetTreeRelationCall", {
					Relation: this.gxval,
					BasicsRelation: basicsRelation
				}).then(data => {
					that.gxname = data.call
				})
			},
			// 在树谱中添加新用户
			AddTreeMerber: function() {
				if (this.xingStr && this.xingStr) {
					if (this.xingStr.length > 2) {
						uni.showToast({
							title: "您填写的姓氏超过了两个字",
							icon: "none"
						})
						return
					}
					if (this.mingStr.length > 4) {
						uni.showToast({
							title: "您填写的名字超过了四个字",
							icon: "none"
						})
						return
					}
					var that = this
					var info = {
						is_living: this.isAlive ? "1" : "0",
						ancepresent: this.isAlive ? this.xianjuAddress : this.mudiAddress
					}
					if (this.borTime && this.borTime.includes("年") &&
						this.borTime.includes("月") && this.borTime.includes("日")) {
						info.birthday = this.borTime1
					}
					if (this.borTime1) {
						info.birthday1 = this.borTime1
					}
					info.birthday2 = this.borTime2
					info.is_lunar_bir = this.borTim3
					if (this.annTime && this.annTime.includes("年") &&
						this.annTime.includes("月") && this.annTime.includes("日")) {
						info.wedding = this.annTime1
					}
					if (this.annTime1) {
						info.wedding1 = this.annTime1
					}
					info.wedding2 = this.annTime2
					info.is_lunar_wed = this.annTime3
					if (this.zhiye) {
						info.occupation = this.zhiye
					}
					if (this.dadTime && this.dadTime.includes("年") &&
						this.dadTime.includes("月") && this.dadTime.includes("日")) {
						info.dieday = this.dadTime1
					}
					if (this.dadTime1) {
						info.dieday1 = this.dadTime1
					}
					info.dieday2 = this.dadTime2
					info.is_lunar_die = this.dadTime3
					if (this.zujiAddress) {
						info.ancestral = this.zujiAddress
					}
					if (this.headImgUpload) {
						info.head = this.headImgUpload
					}
					var upData = {
						TreeId: this.treeId,
						surname: this.xingStr,
						name: this.mingStr,
						// 关系值
						val: this.relation,
						optional: JSON.stringify(info)
					};
					if (this.selectId && this.selectId != '-1' && this.selectId != "undefined") {
						this.$set(upData, 'id', this.selectId)
					}
					request.post("api/apiv2/AddTreeMerber", upData).then(data => {
						uni.$emit("refrashTree")
						uni.navigateBack({
							delta: 1
						});
					})
				} else {
					uni.showToast({
						title: "请填写姓名",
						duration: 1000,
						icon: "none"
					})
				}
			},
			changeZhiYe: function() {
				this.isZhiYeDialogShow = true
			},
			xingInput: function(event) {
				if (!event.target.value) {
					uni.showToast({
						title: "姓氏不能为空",
						duration: 1000,
						icon: "none"
					})
					return
				}
				this.xingStr = event.target.value
			},
			mingInput: function(event) {
				if (!event.target.value) {
					uni.showToast({
						title: "名字不能为空",
						duration: 1000,
						icon: "none"
					})
					return
				}
				this.mingStr = event.target.value
			},
			addressReturn: function(e) {
				this.isChoseAddressShow = e.isclose
				if (e.tag) {
					if (e.address) {
						switch (this.addressType) {
							case "4": //祖籍地
								this.zujiAddress = e.address
								break;
							case "5": //现居地
								this.xianjuAddress = e.address
								break;
							case "6": //墓地
								this.mudiAddress = e.address
								break;
						}
					} else {
						switch (this.addressType) {
							case "4": //祖籍地
								this.zujiAddress = ""
								break;
							case "5": //现居地
								this.xianjuAddress = ""
								break;
							case "6": //墓地
								this.mudiAddress = ""
								break;
						}
					}
				}
			},
			timeReturn: function(e) {
				console.log("timeReturn1111:" + JSON.stringify(e))
				if (e.startTime) {
					switch (this.timeType) {
						case "1":
							//是否是输入
							this.borTim2 = e.timeType
							//阴历阳历
							this.borTim3 = e.dateType
							//需要展示的时间 
							if (e.timeType == 0) {
								var borTimetxt = e.startTime.split("-")
								var borTimeNongLi = getLunar(borTimetxt[0], borTimetxt[1], borTimetxt[2])
								if (e.dateType == 1) {
									//阴历
									this.borTime = borTimetxt[0] + "年" + borTimetxt[1] + "月" + borTimetxt[2] + "日" + "(" + borTimeNongLi.lunarYear +
										" " + borTimeNongLi.dateStr + ")"
								} else {
									//阳历
									this.borTime = borTimetxt[0] + "年" + borTimetxt[1] + "月" + borTimetxt[2] + "日"
								}
							} else {
								this.borTime = e.startTime
							}
							//需要上传的时间
							this.borTime1 = e.startTime
							break;
						case "2":
							//是否是输入
							this.annTime2 = e.timeType
							//阴历阳历
							this.annTime3 = e.dateType
							//需要展示的时间
							if (e.timeType == 0) {
								var annTimetxt = e.startTime.split("-")
								var annTimeNongLi = getLunar(annTimetxt[0], annTimetxt[1], annTimetxt[2])
								if (e.dateType == 1) {
									//阴历
									this.annTime = annTimetxt[0] + "年" + annTimetxt[1] + "月" + annTimetxt[2] + "日" + "(" + annTimeNongLi.lunarYear +
										" " + annTimeNongLi.dateStr + ")"
								} else {
									//阳历
									this.annTime = annTimetxt[0] + "年" + annTimetxt[1] + "月" + annTimetxt[2] + "日"
								}
							} else {
								this.annTime = e.startTime
							}
							//需要上传的时间
							this.annTime1 = e.startTime
							break;
						case "3":
							//是否是输入
							this.dadTime2 = e.timeType
							//阴历阳历
							this.dadTime3 = e.dateType
							//需要展示的时间
							if (e.timeType == 0) {
								var dadTimetxt = e.startTime.split("-")
								var dadTimeNongLi = getLunar(dadTimetxt[0], dadTimetxt[1], dadTimetxt[2])
								if (e.dateType == 1) {
									//阴历
									this.dadTime = dadTimetxt[0] + "年" + dadTimetxt[1] + "月" + dadTimetxt[2] + "日" + "(" + dadTimeNongLi.lunarYear +
										" " + dadTimeNongLi.dateStr + ")"
								} else {
									//阳历
									this.dadTime = dadTimetxt[0] + "年" + dadTimetxt[1] + "月" + dadTimetxt[2] + "日"
								}
							} else {
								this.dadTime = e.startTime
							}
							//需要上传的时间
							this.dadTime1 = e.startTime
							break;
					}
				} else {
					switch (this.timeType) {
						case "1":
							this.borTim2 = e.timeType
							//阴历阳历
							this.borTim3 = e.dateType
							this.borTime = ""
							//需要上传的时间
							this.borTime1 = e.startTime
							break;
						case "2":
							//是否是输入
							this.annTime2 = e.timeType
							//阴历阳历
							this.annTime3 = e.dateType
							this.annTime = e.startTime
							this.annTime1 = e.startTime
							break;
						case "3":
							//是否是输入
							this.dadTime2 = e.timeType
							//阴历阳历
							this.dadTime3 = e.dateType
							this.dadTime = e.startTime
							this.dadTime1 = e.startTime
							break;
					}
				}
			},
			switch2Change: function(e) {
				this.isAlive = e.target.value
			},
			timeSelect: function(e) {
				var that = this
				this.timeType = e.currentTarget.dataset.item;
				uni.navigateTo({
					url: "../choose-time/index"
				})
			},
			addressSelect: function(e) {
				this.isChoseAddressShow = true
				this.addressType = e.currentTarget.dataset.item;
			}
		}
	}
</script>

<style>
	@import "index.css";
</style>
